package com.example.petshopping.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.adapter.ServiceAdapter;
import com.example.petshopping.model.Service;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 上门服务页面活动类
 */
public class DoorToDoorServiceActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private RecyclerView recyclerServices;
    private ServiceAdapter adapter;
    private List<Service> serviceList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_door_to_door_service);

        initViews();
        initData();
        setupRecyclerView();
        setupListeners();
    }

    private void initViews() {
        tabLayout = findViewById(R.id.tab_layout);
        recyclerServices = findViewById(R.id.recycler_services);
        
        ImageView btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> finish());
    }
    
    private void setupListeners() {
        // 设置标签选择监听器
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position) {
                    case 0:
                        // 宠物洗澡页面（当前选中）
                        loadPetBathServices();
                        break;
                    case 1:
                        // 鱼缸造景页面
                        loadFishTankLandscapingServices();
                        break;
                    case 2:
                        // 鱼缸维修页面
                        loadFishTankMaintenanceServices();
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 不需要处理
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 不需要处理
            }
        });
    }
    
    private void initData() {
        serviceList = new ArrayList<>();
        // 初始加载宠物洗澡服务
        loadPetBathServices();
    }

    private void setupRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerServices.setLayoutManager(layoutManager);
        
        adapter = new ServiceAdapter(this, serviceList);
        recyclerServices.setAdapter(adapter);
        
        // 设置点击事件
        adapter.setOnItemClickListener(new ServiceAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Service service, int position) {
                Toast.makeText(DoorToDoorServiceActivity.this, 
                        "点击了服务卡片：" + service.getShopName(), Toast.LENGTH_SHORT).show();
            }
            
            @Override
            public void onEnterClick(Service service, int position) {
                Toast.makeText(DoorToDoorServiceActivity.this, 
                        "进入店铺：" + service.getShopName(), Toast.LENGTH_SHORT).show();
                // 这里可以添加跳转到店铺详情页的代码
            }
            
            @Override
            public void onAddressClick(Service service, int position) {
                Toast.makeText(DoorToDoorServiceActivity.this, 
                        "查看地址：" + service.getAddress(), Toast.LENGTH_SHORT).show();
                // 这里可以添加查看地址的代码，比如打开地图
            }
        });
    }
    
    /**
     * 加载宠物洗澡服务数据
     */
    private void loadPetBathServices() {
        // 清除现有数据
        serviceList.clear();
        
        // 添加宠物洗澡服务示例数据
        Service service1 = new Service(
                "招财猫旺财狗", 
                "pet1", 
                "★★★★★", 
                "44万粉丝", 
                "历下区经十路与舜义路碧桂园凤凰中心b座116室", 
                "距离1.2千米");
        service1.addServiceItem("￥50", "【猫咪洗澡】猫咪基础洗护，不限体重");
        service1.addServiceItem("￥50", "【狗狗洗澡】狗狗基础洗护，0-1.5kg");
        serviceList.add(service1);
        
        Service service2 = new Service(
                "宠悦宠物护理", 
                "pet2", 
                "★★★★☆", 
                "32万粉丝", 
                "历城区山大路与经十路交叉口万达广场4楼", 
                "距离2.3千米");
        service2.addServiceItem("￥60", "【精致洗澡】猫咪精致洗护，不限体重");
        service2.addServiceItem("￥45", "【基础洗护】小型犬洗澡套餐");
        serviceList.add(service2);
        
        Service service3 = new Service(
                "萌宠家园", 
                "pet3", 
                "★★★★★", 
                "28万粉丝", 
                "天桥区明湖西路与堤口路交叉口向南100米", 
                "距离3.1千米");
        service3.addServiceItem("￥55", "【猫咪SPA】专业猫咪洗护，去异味");
        service3.addServiceItem("￥70", "【狗狗SPA】中大型犬基础洗护");
        serviceList.add(service3);
        
        // 通知适配器数据更新
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
    
    /**
     * 加载鱼缸造景服务数据
     */
    private void loadFishTankLandscapingServices() {
        // 清除现有数据
        serviceList.clear();
        
        // 添加鱼缸造景服务示例数据
        Service service1 = new Service(
                "水族天下", 
                "yugang1", 
                "★★★★★", 
                "25万粉丝", 
                "高新区新泺大街与舜华路交叉口东北角", 
                "距离2.7千米");
        service1.addServiceItem("￥150", "【基础造景】1米以内鱼缸标准造景");
        service1.addServiceItem("￥280", "【高级造景】1.2米以内鱼缸高级造景");
        serviceList.add(service1);
        
        Service service2 = new Service(
                "海底世界", 
                "yugang2", 
                "★★★★☆", 
                "18万粉丝", 
                "槐荫区经六路与纬十二路交叉口西南角", 
                "距离3.5千米");
        service2.addServiceItem("￥200", "【专业造景】热带鱼缸专业造景");
        service2.addServiceItem("￥300", "【高端造景】日式风格水草缸造景");
        serviceList.add(service2);
        
        // 通知适配器数据更新
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
    
    /**
     * 加载鱼缸维修服务数据
     */
    private void loadFishTankMaintenanceServices() {
        // 清除现有数据
        serviceList.clear();
        
        // 添加鱼缸维修服务示例数据
        Service service1 = new Service(
                "鱼乐水族", 
                "weixiu1", 
                "★★★★★", 
                "20万粉丝", 
                "市中区英雄山路与马鞍山路交叉口北100米", 
                "距离1.8千米");
        service1.addServiceItem("￥80", "【滤水系统】鱼缸滤水系统维修");
        service1.addServiceItem("￥50", "【水泵更换】鱼缸水泵更换服务");
        serviceList.add(service1);
        
        Service service2 = new Service(
                "海蓝水族", 
                "weixiu2", 
                "★★★★★", 
                "15万粉丝", 
                "历下区解放路与泺源大街交叉口东北角", 
                "距离2.5千米");
        service2.addServiceItem("￥100", "【漏水修复】鱼缸漏水检测修复");
        service2.addServiceItem("￥120", "【灯光系统】灯光系统维修更换");
        serviceList.add(service2);
        
        // 通知适配器数据更新
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
} 